package es.udc.pfc.davidparedes.web.pages.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.universe.Universe;
import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;
import es.udc.pfc.davidparedes.model.userprofile.UserProfile;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.GlobalNames;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.MANAGER_OR_ABOVE)
public class UniverseSelection {
	
	@Property
	private UserProfile userProfile;
	
	@Property
	private Universe universe;
	
	@Property
	private List<Universe> universes;
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private StoreService storeService;
    
    void onActivate() throws InstanceNotFoundException {
    	universes = new ArrayList<Universe>();
		userProfile = userService.findUserProfile(userSession.getUserProfileId());
		if (userProfile instanceof UniverseManager) {
			UniverseManager manager = (UniverseManager) userProfile;
			universes.add(manager.getUniverse());
		} else {
			universes.addAll(userProfile.getStore().getUniverses());
		}
		
    }
    
    public boolean isDirector() {
   		if (userSession.getUserType().equals(GlobalNames.USER_TYPE_DIRECTOR)) {
   			return true;
   		}
        return false;
	}

}
